package com.sdy.supermarket.mq.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sdy.supermarket.biz.model.GsAppIpApply;
import com.sdy.supermarket.biz.model.GsOrderItem;
import com.sdy.supermarket.biz.service.GsAppIpApplyService;
import com.sdy.supermarket.biz.service.GsOrderItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author hyh
 */
@Service
@Slf4j
public class ApprovalMsgFacade {

    @Autowired
    private GsOrderItemService gsOrderItemService;
    @Autowired
    private GsAppIpApplyService gsAppIpApplyService;

    /**
     * 接收到从流程审批模块发送过来的json数据(里面有orderNo(订单号)、resId(资源ID)、state(资源状态))
     * 判断接收到的数据是IP修改单还是需求申请单,然后进行对应操作
     *
     * @param orderInfo
     * @return
     */
    public boolean setOrderAndResIdState(String orderInfo) {
        boolean updateState = true;
        try {
            Map<String, Object> map = JSON.parseObject(orderInfo, Map.class);
            String orderItemNo = (String) map.get("applicationNum");
            Integer state = (Integer) map.get("gsOrderState");
            String resId = (String) map.get("resId");
            if (resId.indexOf("政务超市") == -1) {
                return true;
            }
            if ("[政务超市]1024".equals(resId)) {
                updateState = gsAppIpApplyService.update(Wrappers.<GsAppIpApply>lambdaUpdate().eq(GsAppIpApply::getApplyNo, orderItemNo).set(GsAppIpApply::getState, state));
            } else {
                updateState = gsOrderItemService.update(Wrappers.<GsOrderItem>lambdaUpdate().eq(GsOrderItem::getOrderItemNo, orderItemNo).set(GsOrderItem::getState, state));
            }
        } catch (Exception e) {
            log.error("根据流程审批模块发送的消息更新订单资源状态时出现错误" + e);
        }
        return updateState;
    }
}
